package com.example.image;

import android.graphics.Bitmap;

import java.nio.ByteBuffer;

/**
 * @ClassName BitMaputils
 * @Description 位图处理工具类
 * @Author Spica2 7
 * @Date 2021/6/25 14:00
 */
public class BitmapUtils {


    public static byte[] bitmapToRgba(Bitmap bitmap) {
        if (bitmap.getConfig() != Bitmap.Config.ARGB_8888)
            throw new IllegalArgumentException("Bitmap must be in ARGB_8888 format");
        int bytes = bitmap.getByteCount();  //返回可用于储存此位图像素的最小字节数

        ByteBuffer buffer = ByteBuffer.allocate(bytes); //  使用allocate()静态方法创建字节缓冲区
        bitmap.copyPixelsToBuffer(buffer); // 将位图的像素复制到指定的缓冲区

        // byte[] pixels = new byte[rgba.length];
        //
        // int count = rgba.length / 4;
        //
        // //Bitmap像素点的色彩通道排列顺序是RGBA
        // for (int i = 0; i < count; i++) {
        //
        //     pixels[i * 4] = rgba[i * 4];        //R
        //     pixels[i * 4 + 1] = rgba[i * 4 + 1];    //G
        //     pixels[i * 4 + 2] = rgba[i * 4 + 2];       //B
        //     pixels[i * 4 + 3] = rgba[i * 4 + 3];       //A
        // }

        return buffer.array();
    }

    public static Bitmap bitmapFromRgba(int width, int height, byte[] bytes) {
        int[] colors = convertByteToColor(bytes);    //取RGB值转换为int数组
        if (colors == null) {
            return null;
        }

        Bitmap bmp = Bitmap.createBitmap(colors, 0, width, width, height,
            Bitmap.Config.ARGB_8888);
        return bmp;
    }

    public static Bitmap bitmapFromRgba2(int width, int height, byte[] bytes) {
        int[] pixels = new int[bytes.length / 4];
        int j = 0;

        for (int i = 0; i < pixels.length; i++) {
            int R = bytes[j++] & 0xff;
            int G = bytes[j++] & 0xff;
            int B = bytes[j++] & 0xff;
            int A = bytes[j++] & 0xff;

            int pixel = (A << 24) | (R << 16) | (G << 8) | B;
            pixels[i] = pixel;
        }

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        return bitmap;
    }

    // 将一个byte数转成int
    // 实现这个函数的目的是为了将byte数当成无符号的变量去转化成int
    public static int convertByteToInt(byte data) {

        int heightBit = (int) ((data >> 4) & 0x0F);
        int lowBit = (int) (0x0F & data);
        return heightBit * 16 + lowBit;
    }


    // 将纯RGBA数据数组转化成int像素数组
    public static int[] convertByteToColor(byte[] data) {
        int size = data.length;
        if (size == 0) {
            return null;
        }
        int arg = 0;
        if (size % 4 != 0) {
            arg = 1;
        }
        // 一般RGBA字节数组的长度应该是3的倍数，
        // 不排除有特殊情况，多余的RGBA数据用黑色0XFF000000填充
        int[] color = new int[size / 4 + arg];
        int red, green, blue, alpha;
        int colorLen = color.length;
        if (arg == 0) {

            for (int i = 0; i < colorLen; ++i) {
                red = convertByteToInt(data[i * 4]);
                green = convertByteToInt(data[i * 4 + 1]);
                blue = convertByteToInt(data[i * 4 + 2]);
                alpha = convertByteToInt(data[i * 4 + 3]);
                // 获取RGBA分量值通过按位或生成int的像素值
                color[i] = (alpha << 24) | (red << 16) | (green << 8) | blue | 0xFF000000;
            }
        } else {
            for (int i = 0; i < colorLen - 1; ++i) {
                red = convertByteToInt(data[i * 4]);
                green = convertByteToInt(data[i * 4 + 1]);
                blue = convertByteToInt(data[i * 4 + 2]);
                alpha = convertByteToInt(data[i * 4 + 3]);
                color[i] = (alpha << 24) | (red << 16) | (green << 8) | blue | 0xFF000000;
            }

            color[colorLen - 1] = 0xFF000000;
        }

        return color;
    }

}
